package model.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.dao.MovieDao;
import model.dao.MyCinemaDao;
import model.vo.mycinema.BoardVO;
import model.vo.mycinema.ListVO;
import model.vo.mycinema.ListVOGenericMap;
import model.vo.mycinema.MemberVO;
import model.vo.mycinema.PagingBean;

public class MyCinemaService {
	private MyCinemaDao myCinemaDao;
	private MovieDao movieDao;
	public void setMovieDao(MovieDao movieDao){
		this.movieDao=movieDao;
	}
	public void setMyCinemaDao(MyCinemaDao myCinemaDao) {
		this.myCinemaDao = myCinemaDao;
	}
	public void registerMember(MemberVO vo) throws SQLException{
		System.out.println("멤버");
		myCinemaDao.registerMember(vo);
	}
	public boolean idcheck(String id) throws SQLException {
		boolean result=false;
		if(myCinemaDao.idcheck(id)!=null)
			result=true;
		return result;
	}
	public MemberVO login(MemberVO vo) throws SQLException{
		Object obj = myCinemaDao.login(vo);
		MemberVO mvo = null;
		if(obj!=null){
			mvo = (MemberVO)obj;
		}
		return mvo;
	}
	public ListVO getBoardList(String pageNo) throws SQLException{
		//System.out.println("넌 왜??");
		if(pageNo==null||pageNo=="")  // 목록보기 버튼 누를 경우 최신 글을 보여주기 위해 setting 
			pageNo="1";
		List<BoardVO> list=myCinemaDao.getBoardList(pageNo);
		//System.out.println(list+"목록");
		int total=myCinemaDao.totalContent();
		PagingBean paging=new PagingBean(total,Integer.parseInt(pageNo));
		ListVO lvo=new ListVO(list,paging);
		//System.out.println(lvo+"목록보기");
		return lvo;
	}
	public void write(BoardVO bvo) throws SQLException{
		int no=myCinemaDao.write(bvo);
		String date=myCinemaDao.selectByNoForDate(no);
		bvo.setWrite_date(date);
		System.out.println(bvo+"글쓰기 in service");
	}	
	public BoardVO show(String board_no) throws SQLException{
		System.out.println("내용보기");
		System.out.println(board_no+" "+"show");
		myCinemaDao.updateCount(board_no);
		//System.out.println("show 이동");
		return myCinemaDao.show(board_no);
	}

	public BoardVO showNoCount(String no) throws SQLException{
		return myCinemaDao.show(no);
	}
	public void updateBoard(BoardVO bvo) throws SQLException{
		//System.out.println("바꾸자!!");
		myCinemaDao.updateBoard(bvo);
	}
	public void deleteBoard(String no) throws SQLException{
		myCinemaDao.deleteBoard(no);
	}
	public void reply(BoardVO bvo) throws SQLException{
		//System.out.println("작동 중입니다.");
		//System.out.println(bvo+"reply");		
		int ref = bvo.getRef();
		int restep = bvo.getRestep();
		int relevel = bvo.getRelevel();
		/*System.out.println(ref+"reply");
		System.out.println(restep+""+"reply+");
		System.out.println(relevel+""+"reply");*/
		myCinemaDao.updateRestep(ref, restep);
		//update 처리 후 입력 전에 restep값 과 relevel의 값을 1증가 한다.
		//- 즉 원본 글의 것 보다 1씩 커야 하므로.
		bvo.setRestep(restep+1);
		bvo.setRelevel(relevel+1);
		int no = myCinemaDao.insertRefContent(bvo);//답변 글 입력	
		bvo.setBoard_no(no);		
		String date=myCinemaDao.selectByNoForDate(no);
		bvo.setWrite_date(date);				
	}
	public void updateMember(MemberVO vo) throws SQLException{
		myCinemaDao.updateMember(vo);
	}
	public MemberVO findId(MemberVO vo) throws SQLException{
		return (MemberVO) myCinemaDao.findId(vo);
	}
	public MemberVO findPa(MemberVO vo) throws SQLException{
		return (MemberVO) myCinemaDao.findPa(vo);
	}
	public boolean ssnCheck(String ssn) throws SQLException {
		boolean result=false;
		if(myCinemaDao.ssnCheck(ssn)!=null)
			result=true;
		return result;
	}
	public void deleteMember(MemberVO vo, int member_no) throws SQLException{
		System.out.println("delete+service");
		myCinemaDao.deleteReservationMember(member_no);
		myCinemaDao.deleteBoardMember(member_no);
		myCinemaDao.deleteMember(vo);
	}
	public ListVOGenericMap getAllMyHistory(Map map) {
		List<Map> list = null;
		ListVOGenericMap lvo = null;
		try {
			list = myCinemaDao.getAllMyHistory(map);
			int member_no = (int) map.get("member_no");
			int total = myCinemaDao.getTotalAllMyHistory(member_no);
			String page_no = (String) map.get("page");
			PagingBean paging = new PagingBean(total, Integer.parseInt(page_no));
			lvo = new ListVOGenericMap(list, paging);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lvo;
	}
	public ListVOGenericMap getBuyMyHistory(Map map) {
		List<Map> list = null;
		ListVOGenericMap lvo = null;
		try {
			list = myCinemaDao.getBuyMyHistory(map);
			int member_no = (int) map.get("member_no");
			int total = myCinemaDao.getTotalBuyMyHistory(member_no);
			String page_no = (String) map.get("page");
			PagingBean paging = new PagingBean(total, Integer.parseInt(page_no));
			lvo = new ListVOGenericMap(list, paging);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lvo;
	}
	public ListVOGenericMap getSellMyHistory(Map map) {
		List<Map> list = null;
		ListVOGenericMap lvo = null;
		try {
			list = myCinemaDao.getSellMyHistory(map);
			int member_no = (int) map.get("member_no");
			int total = myCinemaDao.getTotalSellMyHistory(member_no);
			String page_no = (String) map.get("page");
			PagingBean paging = new PagingBean(total, Integer.parseInt(page_no));
			lvo = new ListVOGenericMap(list, paging);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lvo;
	}
	/**
	 * 영화검색
	 */
	public ArrayList<HashMap> searchMovie(String search_title) throws SQLException {
		return movieDao.searchMovie(search_title);
	}
}